package daily.play1;

//public class demo15 {
//    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\java-code\\daily-java\\src\\\\daily\\play1\\copy.txt"));
//        ArrayList list = (ArrayList) ois.readObject();
//        for(int i=0;i<list.size();i++){
//            System.out.println(list.get(i));
//        }
//        ois.close();
//    }
//}

//线程的第三种实现方式

//class MyCallable implements Callable<Integer>{
//
//    @Override
//    public Integer call() throws Exception {
//        int sum = 0;
//        for (int i = 0; i < 100; i++) {
//            sum+=i;
//        }
//        return sum;
//    }
//}
//public class demo15{
//    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        MyCallable mc = new MyCallable();
//        FutureTask<Integer> ft = new FutureTask<>(mc);
//        new Thread(ft).start();
//        System.out.println(ft.get());
//    }
//}

//public class demo15{
//    public static void main(String[] args){
//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 10; i++) System.out.println(Thread.currentThread().getName());
//            }
//        });
//        thread.start();
//        Thread t = Thread.currentThread();
//        System.out.println(t.getName());
//    }
//}
/*public class demo15{
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName());
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName());
                }

            }
        });
        thread1.setName("线程1");
        thread2.setName("线程2");
        thread1.setPriority(10);
        thread2.setPriority(1);
        thread1.start();
        thread2.start();
        System.out.println(thread1.getPriority());
        System.out.println(thread2.getPriority());
    }
}*/

/*public class demo15{
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(Thread.currentThread().getName());
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName()+"@"+i);
                }
            }
        });
        thread2.setDaemon(true);
        thread1.start();
        thread2.start();
    }
}*/
/*public class demo15{
    public static void main(String[] args){
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName());
                    Thread.yield();
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName());
                    Thread.yield();
                }
            }
        });
        thread1.start();
        thread2.start();
    }
}*/
/*public class demo15{
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName()+i);
                }
            }
        });
        thread1.setName("线程1");
        thread1.start();
        thread1.join();
        for (int s = 0; s < 100; s++) {
            System.out.println(s);
        }
    }
}*/

//同步代码块
/*class MyThread extends Thread{
    public static int sum = 100;
    *//*
    * synchronized中的锁对象可以是任意的
    * 但是必须保证对于类的每一个对象是唯一的
    * 所以要使用static关键字修饰
    * *//*
    static Object obj = new Object();
    public void run(){
        int time = 0;
        for (int i = 0; i < 100; i++) {
            synchronized (obj){
                if(sum>0){
                    time++;
                    System.out.println(getName()+"已经卖了"+time+"张");
                    try {
                        this.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    sum--;
                }else{
                    System.out.println(getName()+"票卖完了");
                    break;
                }
            }
        }
    }
}
public class demo15{
    public static void main(String[] args){
        MyThread thread1 = new MyThread();
        MyThread thread2 = new MyThread();
        MyThread thread3 = new MyThread();
        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}*/

//同步方法实现
/*
class MyThread implements Runnable{
    static int sum=100;
    public void run(){
        int time = 0;
        for (int i = 0; i < 100; i++) {
            synchronized (this){
                if(sum>0){
                    time++;

                    System.out.println(Thread.currentThread().getName()+"已经卖了"+time+"张票");
                    sum--;
                }else{
                    System.out.println("票已经卖完了");
                    break;
                }
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class demo15{
    public static void main(String[] args) {
        MyThread thread  = new MyThread();
        Thread thread1 = new Thread(thread);
        Thread thread2 = new Thread(thread);
        Thread thread3 = new Thread(thread);

        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");

        thread2.start();
        thread3.start();
        thread1.start();

    }
}
*/

//同步方法
//class MyThread implements Runnable{
//    static int sum=100;
//    public void run(){
//        temp();
//    }
//    private void temp() {
//        int time = 0;
//        for (int i = 0; i < 100; i++) {
//            synchronized (this){
//                if(sum>0){
//                    time++;
//
//                    System.out.println(Thread.currentThread().getName()+"已经卖了"+time+"张票");
//                    sum--;
//                }else{
//                    System.out.println("票已经卖完了");
//                    break;
//                }
//            }
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
//    }
//}
//public class demo15{
//    public static void main(String[] args) {
//        MyThread thread  = new MyThread();
//        Thread thread1 = new Thread(thread);
//        Thread thread2 = new Thread(thread);
//        Thread thread3 = new Thread(thread);
//
//        thread1.setName("窗口1");
//        thread2.setName("窗口2");
//        thread3.setName("窗口3");
//
//        thread2.start();
//        thread3.start();
//        thread1.start();
//
//    }
//}

//同步方法
//class MyRunnable implements Runnable{
//
//    int ticket = 0;
//    @Override
//    public void run() {
//        for (int i = 0; i < 100; i++) {
//          buy();
//        }
//    }
//    public synchronized void buy(){
//        if(ticket==100){
//            return ;
//        }else{
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            ticket++;
//            System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket+"票");
//        }
//    }
//}
//public class demo15{
//    public static void main(String[] args){
//        MyRunnable mr = new MyRunnable();
//        Thread t1 = new Thread(mr);
//        Thread t2 = new Thread(mr);
//        Thread t3 = new Thread(mr);
//        t1.setName("窗口1");
//        t2.setName("窗口2");
//        t3.setName("窗口3");
//        t1.start();
//        t2.start();
//        t3.start();
//    }
//}

//lock锁
/*class Myclass implements Runnable{
  int stick = 0;
  Lock lock = new ReentrantLock();
    @Override
    public void run() {
        for(int i=0;i<100;i++){
            lock.lock();
            try {
            if(stick==100){
                break;
            }else{
                Thread.sleep(10);
                stick++;
                System.out.println(Thread.currentThread().getName()+"正在卖第"+stick+"张票");
            }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally{
                lock.unlock();
            }
        }
    }
}*/


/*public class demo15{
    public static void main(String[] args){
        Myclass mc = new Myclass();
        Thread t1 = new Thread(mc);
        Thread t2 = new Thread(mc);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t1.start();
        t2.start();
    }*/
//继承thread类的方式
/*class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println(getName()+"正在学习多线程");
    }
}
public class demo15{
    public static void main(String[] args){
        Random random = new Random();
        int a  = random.nextInt(100,101);
        System.out.println(a);
            MyThread thread1 = new MyThread();
            MyThread thread2 = new MyThread();
            thread1.setName("线程1");
            thread2.setName("线程2");
            thread1.start();
            thread2.start();
    }
}*/

//声明实现runnable接口
/*class Mythread implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()"正在学习多线程");
    }
}
public class demo15{
    public static void main(String[] args){
        Mythread thread1 = new Mythread();
        Mythread thread2 = new Mythread();
        Thread thread3 = new Thread(thread1);
        Thread thread4 = new Thread(thread2);
        thread3.start();
        thread4.start();
    }
}*/

//callable接口和future接口
/*class MyCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        int sum=0;
        for (int i = 1; i <= 100; i++) {
            sum+=i;
        }
        return sum;
    }
}
public class  demo15{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable mc =  new MyCallable();
        FutureTask<Integer> ft = new FutureTask<>(mc);
        Thread t = new Thread(ft);
        t.start();
        Integer result = ft.get();
        System.out.println(result);
    }
}*/
/*class Parent{
    int a;
    public Parent(int a){
        this.a = a;
    }
    public Parent() {

    }
}
class Child extends Parent{
    int b;
    public Child(int b){
        this.b = b;
    }
}
public class demo15{
    public static void main(String[] args) {
//        Thread thread = new Thread();
//        System.out.println(thread.getName());
    }
}*/

//线程优先级
/*class Mythread implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("我是"+Thread.currentThread().getName()+"Hello World");
        }
    }
}
public class demo15{
    public static void main(String[] args) {
        Mythread mt = new Mythread();
        Thread thread1 = new Thread(mt);
        Thread thread2 = new Thread(mt);
        thread1.setName("一号小羊");
        thread2.setName("二号小羊");
        System.out.println(thread1.getPriority());
        System.out.println(thread2.getPriority());
        thread1.setPriority(1);
        thread2.setPriority(10);
        thread1.start();
        thread2.start();

    }
}*/

//守护线程
/*class Mythread1 extends Thread{
    public void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println(getName()+"@"+i);
        }
    }
}
class Mythread2 extends Thread{
    public void run(){
        for (int i = 0; i < 100; i++) {
            System.out.println(getName()+"@"+i);
        }
    }
}
public class demo15{
    public static void main(String[] args){
        Mythread1 t1 = new Mythread1();
        Mythread2 t2 = new Mythread2();
        t1.setName("女神");
        t2.setName("备胎");
        t2.setDaemon(true);
        t1.start();
        t2.start();
    }
}*/

//插入线程
/*class MyThread extends Thread{
    public void run(){
        for (int i = 0; i < 100; i++) {
            System.out.println("你好");
        }
    }
}
public class demo15{
    public static void main(String[] args) throws InterruptedException {
        MyThread mt = new MyThread();
        mt.start();
        mt.join();
        for (int i = 0; i < 10; i++) {
            System.out.println("世界");
        }
    }
}*/

//3个窗口卖100张票
/*class MyThread extends Thread{
    static int ticket = 100;
    static int sum = 0;
    static Object obj = new Object();
    public void run() {

        for (int i = 0; i < 100; i++) {
            synchronized (obj) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                sum++;
                ticket--;
                if(ticket<0){
                    break;
                }
                System.out.println(getName()+"正在卖第"+sum+"票");
            }
        }
    }
}

public class demo15{
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();
        MyThread t3 = new MyThread();
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}*/

//模拟厨师做饭流程
//class Cook extends Thread{
//    public void run(){
//        while(true){
//            synchronized (Desk.lock){
//                //如果有食物
//                if(Desk.foolflag==1){
//                    try {
//                        Desk.lock.wait();
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }else{
//                    if(Desk.cnt==0){
//                        break;
//                    }
//                    //如果没有食物
//                    System.out.println("厨师正在做面条，还可以做"+Desk.cnt+"碗！");
//                    //唤醒吃货
//                    Desk.lock.notifyAll();
//                    //修改桌子的状态
//                    Desk.foolflag=1;
//                }
//            }
//        }
//    }
//}
//
//class Foodie extends Thread{
//    public void run(){
//        while(true){
//            synchronized (Desk.lock){
//                //如果桌子上没有面条
//                if(Desk.foolflag==0){
//                    try {
//                        Desk.lock.wait();
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }else{
//                    if(Desk.cnt==0){
//                        break;
//                    }
//                    //如果桌子上有面条
//                    Desk.cnt--;
//                    System.out.println("正在吃面条，还能吃"+Desk.cnt+"碗！");
//                    //唤醒厨师
//                    Desk.lock.notifyAll();
//                    //修改桌子的状态
//                    Desk.foolflag=0;
//                }
//
//            }
//        }
//    }
//}
//
//class Desk {
//    //桌子的状态
//    public static int foolflag = 0;
//    //锁对象
//    public static Object lock = new Object();
//    public static int cnt = 10;
//}
//
//public class demo15{
//    public static void main(String[] args) {
//        Cook cook = new Cook();
//        Foodie foodie = new Foodie();
//        cook.start();
//        foodie.start();
//    }
//}

/*import java.util.concurrent.ArrayBlockingQueue;

//阻塞队列
class Cook extends Thread{
    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
    public Cook(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }
    public void run(){
        while(true){
            try {
                queue.put("蛋糕");
                System.out.println("放了一个蛋糕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class foodie extends Thread{
    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
    public foodie(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }
    public void run(){
        while(true){
            try {
                queue.take();
                System.out.println("消耗一个蛋糕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class demo15{
    public static void main(String [] args){
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        Cook cook = new Cook(queue);
        cook.start();
        foodie foodie = new foodie(queue);
        foodie.start();
    }
}*/

//等待唤醒机制
/*class  Cook extends Thread{
    public void run(){
        while (true){
            synchronized (Desk.lock){
                if(Desk.count==0)
                    break;
                //如果桌子上面没有有蛋糕
                if(Desk.foodFlag==0){
                    try {
                        Desk.lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else{
                    Desk.count--;
                    System.out.println("吃货正在吃蛋糕！还可以吃"+Desk.count+"个");
                    //唤醒厨师
                    Desk.lock.notifyAll();
                    //更行桌子的状态
                    Desk.foodFlag=0;
                }
            }

        }
    }
}
class Foodie extends Thread{
        public void run(){
            while(true){
                synchronized (Desk.lock){
                    if(Desk.count==0){
                        break;
                    }
                    //如果桌子上面有蛋糕
                    if(Desk.foodFlag==1){
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else{
                        System.out.println("厨师做了一个蛋糕");
                        //唤醒吃货
                        Desk.lock.notifyAll();
                        //更新桌子的状态
                        Desk.foodFlag=1;
                    }
                }
            }
        }
}
class Desk{
    //桌子的状态
    static int foodFlag=0;
    //吃货一共可以吃的数量
    static int count =10;
    //一把公用的锁
    static Object lock = new Object();
}

public class demo15{
    public static void main(String[] args) {
        Cook cook = new Cook();
        cook.start();
        Foodie foodie = new Foodie();
        foodie.start();

    }
}*/

import java.util.concurrent.ArrayBlockingQueue;

//阻塞队列
class Cook extends Thread{
    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
    public Cook(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }
    public int sum = 0;
    public void run(){
        while(true){
            if(sum==10){
                break;
            }
            try {
                queue.put("蛋糕");
                System.out.println("厨师放了一个蛋糕");
                sum++;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class Foodie extends Thread{
    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
    int sum=0;
    public Foodie(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }

    public void run(){
        while(true){
            if(sum==10){
                break;
            }
            try {
                queue.take();
                System.out.println("吃货吃了一个蛋糕");
                sum++;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class demo15{
    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
        Cook cook = new Cook(queue);
        Foodie foodie = new Foodie(queue);
        cook.start();
        foodie.start();
    }
}
